﻿--[[
   @brief    contact item node
   @author   perpu
   @version  1.0
   @date     2016-06-15
--]]

contact_group_node = {
    update = function(nodetype, control_id, info)
        if nodetype == contact_list_model.NODE_TYPE_DISCUSSION_NODE or
           nodetype == contact_list_model.NODE_TYPE_GROUP_NODE then
            local group_info = info["group_info"];
            contact_list_model.virtual_node_id_to_group_info[control_id] = group_info;

            local static_id = ui_utility.get_ctrl_id_by_path(control_id, 'Static_Nick_Name');
            local ui_str, str_holder = U8toCUIStr(info.name);
            ui_utility.setproperty_by_id(static_id,'Caption', group_info.name);

            local face_url = string.format("ONEIMAGE:client_social\\group.png|%s;Circle;null;", group_info.face_url);
            if contact_list_model.NODE_TYPE_DISCUSSION_NODE == nodetype then
              face_url = string.format("ONEIMAGE:client_social\\discus.png;Circle;null;");
            end
            local head_id = ui_utility.get_ctrl_id_by_path(control_id, 'Button_Head_Image');
            ui_utility.setproperty_by_id(head_id, "Background", face_url);

            id = ui_utility.get_ctrl_id_by_path(control_id, "Button_Mask_Image");
            if group_info.mask_msg_flag == 0 then
                ui_utility.setproperty_by_id(id, "Visible", "false");
            else
                ui_utility.setproperty_by_id(id, "Visible", "true");
            end
            -- test
            -- ui_utility.setproperty_by_id(id, "Visible", "true");

            --选中态
            if contact_list_model.current_selected_group_id == group_info.group_id then
                contact_group_node.set_selected_state(control_id);
            end
            
        elseif nodetype == contact_list_model.NODE_TYPE_DISCUSSION_TITLE then
            contact_group_node.update_discussion_title(control_id);

        elseif nodetype == contact_list_model.NODE_TYPE_GROUP_TITLE then
            contact_group_node.update_group_title(control_id);
        elseif nodetype == contact_list_model.NODE_TYPE_NO_GROUP_TIPS then
            contact_group_node.update_recommend_group(control_id);
        else 
            --local static_id = ui_utility.get_ctrl_id_by_path(control_id, 'Static_Friend_Name');
            --local ui_str, str_holder = U8toCUIStr(info.name);
            --ui_utility.setproperty_by_id(static_id,'Caption', info.name);
        end;
    end;
    
    update_discussion_title = function(ctrl_id)
        local discuss_cnt = group_list_model.get_favor_discussion_count();
        local my_chart_room = LoadStr("SOCIAL_GROUP_NODE_MYCHARTROOM");
        local str = string.format("%s(%d)", my_chart_room, discuss_cnt);
        ui_utility.setproperty_by_id(ctrl_id, "Caption", str);
        contact_group_node.update_tree_direction_flag(ctrl_id, contact_list_model.b_discussion_cllapsed_);
    end;

    update_group_title = function(ctrl_id)
        local normal_group_cnt = group_list_model.get_group_count_ext();
        local my_group = LoadStr("SOCIAL_GROUP_NODE_MYGROUP");
        local str = string.format("%s(%d)", my_group, normal_group_cnt);
        ui_utility.setproperty_by_id(ctrl_id, "Caption", str);
        contact_group_node.update_tree_direction_flag(ctrl_id, contact_list_model.b_group_cllapsed_);
    end;

    --更新推荐群组节点
    update_recommend_group = function(ctrl_id)
--        if contact_list_model.mainframe_mode == 1 then --mini mode
--            local rec_ctn_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_Recommend");
--            ui_utility.setproperty_by_id(rec_ctn_id, "Visible", "false");
--            return;
--        end

        local p_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_Recommend\\Ctn_222\\RecommendGroupList");
        local loop_cnt = 4;
        local rec_g_list = group_list_model.get_recommend_group();
        if rec_g_list ~= nil then
            if #rec_g_list < loop_cnt then
                loop_cnt = #rec_g_list;
            end
        else
            loop_cnt = 0;
        end

        if loop_cnt == 0 then
            return;
        end

        contact_list_model.join_group_btn_id_to_group_id = {};
        for i = 1, loop_cnt do
            local group = rec_g_list[i];
            local user_obj_id = ui_utility.get_ctrl_id_by_path(p_id, string.format("R_%d", i));
            local group_banner_id = ui_utility.get_ctrl_id_by_path(user_obj_id, "Container_GroupBannerForRecommend");

            local id = ui_utility.get_ctrl_id_by_path(group_banner_id, "Button_Head_Image");
            ui_utility.setproperty_by_id(id, "Background", string.format("ONEIMAGE:social_network\\group-head-small.png|%s;Circle;null;", group["face_url"]));
            
            id = ui_utility.get_ctrl_id_by_path(group_banner_id, "Button_Group_Name");
            ui_utility.setproperty_by_id(id, "Caption", group["name"]);

            local id_join_btn = ui_utility.get_ctrl_id_by_path(group_banner_id, "Button_JoinGroup");
            local flag_join_btn = ui_utility.get_ctrl_id_by_path(group_banner_id, "TinyStatic_AlreadyFlag");
            local is_apply = contact_list_model.is_apply_join_group_id(group["group_id"]);
            if is_apply then
                ui_utility.setproperty_by_id(id_join_btn, "Visible", "false");
                ui_utility.setproperty_by_id(flag_join_btn, "Visible", "true");
            else
                ui_utility.setproperty_by_id(id_join_btn, "Visible", "true");
                ui_utility.setproperty_by_id(flag_join_btn, "Visible", "false");
            end

            contact_list_model.join_group_btn_id_to_group_id[group_banner_id] = group["group_id"];
        end
    end;

    restore_banner_bk = function()
        for k, v in pairs(contact_list_model.virtual_node_id_to_group_info) do
            if contact_list_model.current_hover_group_id ~= v.group_id then
                contact_group_node.set_normal_state(k);
            end
        end
    end;

    --以下是鼠标键盘消息处理
    mouse_over_banner = function(uilib, ctrl_id)
        local g_info = contact_list_model.virtual_node_id_to_group_info[ctrl_id];
        if g_info ~= nil and g_info.group_id ~= contact_list_model.current_selected_group_id then
            contact_group_node.set_hover_state(ctrl_id);
        end
    end;

    mouse_leave_banner = function(uilib, ctrl_id)
        local g_info = contact_list_model.virtual_node_id_to_group_info[ctrl_id];
        if g_info ~= nil and g_info.group_id ~= contact_list_model.current_selected_group_id then
            contact_group_node.set_normal_state(ctrl_id);
        end
    end;

    current_selected_ctrl_id_ = nil;

    on_left_button_down = function(uilib, ctrl_id)
        contact_group_node.current_selected_ctrl_id_ = ctrl_id;
        contact_group_node.set_selected_state(ctrl_id);
    end;

    on_right_button_down = function(uilib, ctrl_id)
    end;

    on_right_button_up = function(uilib, ctrl_id, x, y)
        local group_info = contact_list_model.virtual_node_id_to_group_info[ctrl_id];
        if group_info ~= nil then
            contact_group_node.send_hide_group_tips_event(1);
            local pControl = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
            local rc = pControl:GetRectEx(TRUE,TRUE);
            local param = {};
            param["group_id"] = group_info.group_id;
            param["x"] = rc.left + x;
            param["y"] = rc.top + y;
            event_center.send_event(group_event.SHOW_GROUP_MENU,base_utility.table_to_json(param))
        end        
    end;

    mouse_db_click = function(uilib, ctrl_id)
        local selected = contact_group_node.current_selected_ctrl_id_;
        local group_info = contact_list_model.virtual_node_id_to_group_info[selected];
        if group_info ~= nil then
            local table_param = {};
            local groups = {};
            local info = {};
            info.id = group_info.group_id;
            info.is_new = false;
            if group_info.last_msg_ == nil then
                info.time = 0;
            else
                info.time = group_info.last_msg_.time; 
            end   
            info.group_type = group_info.group_type;
            table.insert(groups, info);
            
            table_param["groups"] = groups;
            event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(table_param));
        end
    end;

    start_show_group_tip_timer = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('contact_group_node.show_group_tip_timer_process', 300);
    end;

    stop_show_group_tip_timer = function()
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('contact_group_node.show_group_tip_timer_process');
    end;

    show_group_tip_timer_process = function()
        contact_group_node.stop_show_group_tip_timer();
        contact_group_node.send_show_group_tips_event();
    end;

    send_show_group_tips_event = function()
        local p_id = ui_utility.get_ctrl_parent_id(contact_group_node.current_hover_header_ctrl_id);
        if p_id == 0 then
            return;
        end
        local  group_info = contact_list_model.virtual_node_id_to_group_info[p_id];
        if group_info == nil then
            return;
        end

        local pControl = ui_utility.get_ctrl_ptr_by_id(contact_group_node.current_hover_header_ctrl_id);
        local rc = pControl:GetRectEx(TRUE,TRUE);
        local param = {};
        param["bShow"] = true;    
        param["group_id"] = group_info.group_id;
        local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
        if out.mode == 0 then
            param["x"] = rc.left - 15;
        else
            param["x"] = rc.left - 17;
        end
        param["y"] = rc.top;
        event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
    end;

    mouse_over_imgage = function(uilib, ctrl_id)
        local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
        local g_info = contact_list_model.virtual_node_id_to_group_info[p_id];

        if group_menu_view == nil or group_menu_view.is_show() == false then
            if g_info ~= nil and g_info.group_type == "Public" then
                contact_group_node.current_hover_header_ctrl_id = ctrl_id;
                contact_group_node.stop_show_group_tip_timer();
                contact_group_node.start_show_group_tip_timer();
            end
        end
        
        if g_info ~= nil and g_info.group_id ~= contact_list_model.current_selected_group_id then
            contact_group_node.set_hover_state(p_id);
        end
    end;

    mouse_leave_imgage = function(uilib, ctrl_id)
        base_utility.log("[contact_group_node] mouse_leave_group_imgage");
        contact_group_node.stop_show_group_tip_timer();

        if group_menu_view == nil or group_menu_view.is_show() == false then
            contact_group_node.send_hide_group_tips_event(0);
        end

        -- local banner = GroupBannerList:get_banner_by_ctrl(control_id);
        -- if banner == nil then
        --     base_utility.log(string.format("[GroupBannerUIEventListener][mouse_over_group]banner is NIL,"), true);
        --     return;
        -- end
        
        -- local selected = banner:get_selected();
        -- if  selected == false then 
        --   banner:set_normal_state();
        -- end
    end;

    on_left_button_up_image = function(uilib, ctrl_id)
        base_utility.log("[contact_group_node] on_left_button_up_image");

        local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
        local g_info = contact_list_model.virtual_node_id_to_group_info[p_id];
        if g_info ~= nil then
            if g_info.group_type == "Public" then
                local param_table = {group_id = g_info.group_id};
                event_center.send_event(group_event.SHOW_GROUP_CENTER, base_utility.table_to_json(param_table));
            end
        end
    end;
    
    on_left_button_down_image = function(uilib, ctrl_id)
        base_utility.log("[contact_group_node] on_left_button_down_image");
        contact_group_node.send_hide_group_tips_event(1);
    end;

    on_right_button_down_image = function(uilib, ctrl_id, x, y)
    end;

    on_right_button_up_image = function(uilib, ctrl_id, x, y)
        local rc = uilib:GetControlPtr(ctrl_id):GetRectEx(FALSE,TRUE);
        local p_ctrl = uilib:GetControlPtr(ctrl_id):GetParent();
        local p_id = p_ctrl:GetControlID();
        contact_group_node.on_right_button_up(uilib, p_id, rc.left + x, rc.top + y);
    end;

    set_normal_state = function(ctrl_id)
        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_bk");
        ui_utility.setproperty_by_id(bk_id, "Background", "");
    end;

    set_hover_state = function(ctrl_id)
        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_bk");
        ui_utility.setproperty_by_id(bk_id, "Background", "COLOR:RGB:229,229,229;");
        local g_info = contact_list_model.virtual_node_id_to_group_info[ctrl_id];
        contact_list_model.current_hover_group_id = g_info.group_id;
    end;

    set_selected_state = function(ctrl_id)
        contact_group_node.restore_banner_bk();
        local bk_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_bk");
        ui_utility.setproperty_by_id(bk_id, "Background", "COLOR:RGB:229,229,229;");
        local g_info = contact_list_model.virtual_node_id_to_group_info[ctrl_id];
        if nil ~= g_info then
            contact_list_model.current_selected_group_id = g_info.group_id;
        end
    end;

    on_left_button_down_discussion_tree = function(uilib, ctrl_id)
        contact_list_control.clicked_treenode();
        contact_group_node.update_tree_direction_flag(ctrl_id, contact_list_model.b_discussion_cllapsed_);
    end;

    on_left_button_down_group_tree = function(uilib, ctrl_id)
        contact_list_control.clicked_grouptreenode();
        contact_group_node.update_tree_direction_flag(ctrl_id, contact_list_model.b_group_cllapsed_);
    end;

    send_hide_group_tips_event = function(action)
        base_utility.log(string.format("[contact_group_node]send_hide_group_tips_event:%d", action));
        local param = {};
        param["bShow"] = false;
        param["action"] = action;
        event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
    end;

    --更新箭头方向
    update_tree_direction_flag = function(ctrl_id, is_cllapsed)
        local ui_str, str_hodler = U8toCUIStr("Direction_Flag_Right");
        local id_right = ui_utility.uilib():GetControlIDbyPath(ui_str, ctrl_id);

        local ui_str, str_hodler = U8toCUIStr("Direction_Flag_Down");
        local id_down = ui_utility.uilib():GetControlIDbyPath(ui_str, ctrl_id);

        if is_cllapsed then
            ui_utility.setproperty_by_id(id_right, "Visible", "true");
            ui_utility.setproperty_by_id(id_down, "Visible", "false");
        else
            ui_utility.setproperty_by_id(id_right, "Visible", "false");
            ui_utility.setproperty_by_id(id_down, "Visible", "true");
        end

    end;

};
